package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;

/**
 *  http://www.coatingol.com/news/  中国涂料在线
 *  xiaonie 2012-11-22
 */
public class CoatingolNewsAnalyse implements AnalyseNews{
	private final String[] regexs={ "http://www.coatingol.com/news/info[0-9]*.htm"};

	
	public boolean isDetailPage(String url) {
		for (String regex : regexs) {
			if (url.matches(regex)) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		news.setUrl(url);
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;

		title=doc.select("font.sub_news_title").text();
		date=DateUtils.matchDate(doc.select("font.text_chinese").text());

		content=HtmlCleaner.getContentHtml(url,doc.select("td.ns_view_tb_content"));

		/*
		 * 作者
		 */
		author=JSoupUtils.matchAuthor(doc, "来源：");

		news.setContent(content);
		news.setTitle(title);
		news.setClickNum(null);
		news.setAuthor(author);
		news.setDate(date);
		news.setType(1);
		return news;
	}

	public static void main(String[] args) {
		CoatingolNewsAnalyse a=new CoatingolNewsAnalyse();
		String url="http://www.coatingol.com/news/info108083.htm";
		System.out.println(DoMainUtils.GetDomainName(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
